package com.kkb.service.dataanalysis.impl;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.kkb.bean.AnthologyTable;
import com.kkb.dao.AnthologyTableMapper;
import com.kkb.myenum.FileOperateEnum;
import com.kkb.bean.OrderTable;
import com.kkb.bean.User;
import com.kkb.dao.FileAnalysisMapper;
import com.kkb.dao.OrderTableMapper;
import com.kkb.service.dataanalysis.DataOverviewService;
import com.kkb.utils.util.DateUtils;
import com.kkb.vo.dataanalysis.HourData;
import com.kkb.vo.dataanalysis.TopProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author mj
 * @create 2021-04-12 19:11
 * @describe:
 */
@Service
@Slf4j
public class DataOverviewServiceImpl implements DataOverviewService {

    @Autowired
    private OrderTableMapper orderTableMapper;

    @Autowired
    private FileAnalysisMapper fileAnalysisMapper;

    @Autowired
    private AnthologyTableMapper anthologyTableMapper;


    /**
     * 查询今日/昨日的每个小时的收入，
     * 查询付费用户数，支付订单数
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> selectRealTimeOverview(User user) {
        Map<String,Object> map = new HashMap<>();
        OrderTable orderTable = new OrderTable();
        log.info("查询今天每一个小时的收入");
        List<HourData> nowData = orderTableMapper.selectHourData(user.getUserId(), DateUtils.dateToStr(new Date()),DateUtils.dateToStr(new Date()));
        map.put("nowDate",nowData);

        log.info("查询昨天每一个小时的收入");
        List<HourData> yesterdayData = orderTableMapper.selectHourData(user.getUserId(), DateUtils.yesterday(),DateUtils.yesterday());
        map.put("yesterdayDate",yesterdayData);

        log.info("查询付费订单信息");
        List<OrderTable> orderTableList = orderTableMapper.selectOrder(user.getUserId(), 1, DateUtils.dateToStr(new Date()));
        Set<Integer> payUserNumSet = new HashSet<>();
        for (OrderTable order : orderTableList) {
            payUserNumSet.add(order.getBuyerId());
        }
        //付费用户数
        map.put("payUserNum",payUserNumSet.size());
        //支付订单数
        map.put("orderPayedNum",orderTableList.size());

        return map;
    }

    /**
     * 查询运营视图页面模块数据，
     * @param user
     * @param date ： 截至天数
     * @return
     */
    @Override
    public Map<String, Object> operationWindow(User user, String date) {
        Map<String,Object> map = new HashMap<>();
        log.info("根据日期查询收入信息");
        List<HourData> income = orderTableMapper.selectHourData(user.getUserId(), date,DateUtils.dateToStr(new Date()));
        map.put("income",income);

        log.info("根据日期查询浏览量信息");
        List<HourData> pageviews = fileAnalysisMapper.selectHourData2(user.getUserId(), date,DateUtils.dateToStr(new Date()), FileOperateEnum.PREVIEWS.getCode());
        map.put("pageviews",pageviews);

        log.info("根据日期查询访客量信息");
        List<HourData> visitorVolume = fileAnalysisMapper.selectHourData2(user.getUserId(), date,DateUtils.dateToStr(new Date()), FileOperateEnum.VISITOR_VOLUME.getCode());
        map.put("visitorVolume",visitorVolume);

        log.info("根据日期查询下载量信息");
        List<HourData> downloadNum = fileAnalysisMapper.selectHourData2(user.getUserId(), date,DateUtils.dateToStr(new Date()), FileOperateEnum.DOWNLOADING_NUM.getCode());
        map.put("downloadNum",downloadNum);

        log.info("根据日期查询支付订单量");
        List<HourData> oderPayedNum = orderTableMapper.selectOrderPayedNum(user.getUserId(), date,DateUtils.dateToStr(new Date()));
        map.put("oderPayedNum",oderPayedNum);

        log.info("根据日期查询付费用户数");
        List<HourData> payedUserNum = orderTableMapper.selectPayedUserNum(user.getUserId(), date,DateUtils.dateToStr(new Date()));
        map.put("payedUserNum",payedUserNum);

        //-----------》******************《-----

        log.info("根据日期查询环比收入信息");
        List<HourData> incomeHuanbi = orderTableMapper.selectHourData(user.getUserId(), DateUtils.parseDateHuanbi(date),date);

        log.info("根据日期查询环比浏览量信息");
        List<HourData> pageviewsHuanbi = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.parseDateHuanbi(date),date, FileOperateEnum.PREVIEWS.getCode());

        log.info("根据日期查询环比访客量信息");
        List<HourData> visitorVolumeHuanbi = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.parseDateHuanbi(date),date, FileOperateEnum.VISITOR_VOLUME.getCode());

        log.info("根据日期查询环比下载量信息");
        List<HourData> downloadNumHuanbi = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.parseDateHuanbi(date),date, FileOperateEnum.DOWNLOADING_NUM.getCode());

        log.info("根据日期查询环比支付订单量");
        List<HourData> oderPayedNumHuanbi = orderTableMapper.selectOrderPayedNum(user.getUserId(), DateUtils.parseDateHuanbi(date),date);

        log.info("根据日期查询环比付费用户数");
        List<HourData> payedUserNumHuanbi = orderTableMapper.selectPayedUserNum(user.getUserId(), DateUtils.parseDateHuanbi(date),date);

        //计算环比值
        map.put("incomeHuanbi",countHuanbi(income,incomeHuanbi,2));
        map.put("pageviewsHuanbi",countHuanbi(pageviews,pageviewsHuanbi,1));
        map.put("visitorVolumeHuanbi",countHuanbi(visitorVolume,visitorVolumeHuanbi,1));
        map.put("downloadNumHuanbi",countHuanbi(downloadNum,downloadNumHuanbi,1));
        map.put("oderPayedNumHuanbi",countHuanbi(oderPayedNum,oderPayedNumHuanbi,1));
        map.put("payedUserNumHuanbi",countHuanbi(payedUserNum,payedUserNumHuanbi,1));

        return map;
    }

    /**
     *
     * @param nowList
     * @param huanbiList
     * @param flag
     * @return
     */
    private double countHuanbi(List<HourData> nowList,List<HourData> huanbiList,int flag){
        double nowNum = 0;
        for (HourData hourData : nowList) {
            if (flag==1) {
                nowNum += hourData.getNum();
            }else {
                nowNum += hourData.getTotalMany();
            }
        }

        double huanbi = 0;
        for (HourData hourData : huanbiList) {
            if (flag==1) {
                huanbi += hourData.getNum();
            } else {
                huanbi += hourData.getTotalMany();
            }
        }

        return huanbi!=0?(nowNum - huanbi)/huanbi:0;
    }

    /**
     * 查询流量看板信息
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> trafficKanban(User user) {
        Map<String,Object> map = new HashMap<>();

        log.info("根据日期查询浏览量信息");
        List<HourData> pageViews = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.dateToStr(new Date()),DateUtils.dateToStr(new Date()), FileOperateEnum.PREVIEWS.getCode());
        map.put("pageviews",pageViews);

        log.info("根据日期查询访客量信息");
        List<HourData> visitorVolume = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.dateToStr(new Date()),DateUtils.dateToStr(new Date()), FileOperateEnum.VISITOR_VOLUME.getCode());
        map.put("visitorVolume",visitorVolume);

        log.info("查询浏昨天览量信息");
        List<HourData> yesterdayPageViews = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.yesterday(),DateUtils.yesterday(), FileOperateEnum.PREVIEWS.getCode());

        log.info("查询昨天访客量信息");
        List<HourData> yesterdayVisitorVolume = fileAnalysisMapper.selectHourData2(user.getUserId(), DateUtils.yesterday(),DateUtils.yesterday(), FileOperateEnum.VISITOR_VOLUME.getCode());

        //获取今日浏览量
        double nowTotalViews = 0;
        for (HourData pageView : pageViews) {
            nowTotalViews += pageView.getNum();
        }
        //获取今日浏览量
        double visitorVolumeTotal = 0;
        for (HourData hourData : visitorVolume) {
            visitorVolumeTotal += hourData.getNum();
        }

        //获取昨日浏览量
        double yesterdayTotalViews = 0;
        for (HourData pageView : yesterdayPageViews) {
            yesterdayTotalViews += pageView.getNum();
        }

        //获取昨日浏览量
        double yesterdayVisitorVolumeTotal = 0;
        for (HourData hourData : yesterdayVisitorVolume) {
            yesterdayVisitorVolumeTotal += hourData.getNum();
        }

        System.out.println((nowTotalViews-yesterdayTotalViews)/yesterdayTotalViews);
        map.put("yesterdayViewsHuanBi",yesterdayTotalViews!=0?((nowTotalViews-yesterdayTotalViews)/yesterdayTotalViews):0);
        map.put("yesterdayUserHuanBi",yesterdayVisitorVolumeTotal!=0?((visitorVolumeTotal-yesterdayVisitorVolumeTotal)/yesterdayVisitorVolumeTotal):0);

        map.put("nowTotalViews",nowTotalViews);
        map.put("visitorVolumeTotal",visitorVolumeTotal);

        return map;
    }

    /**
     * 查询top5的商品/支付
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> top(User user) {
        Map<String,Object> map = new HashMap<>();
        log.info("查询支付榜前5的商品");
        List<TopProductVo> payTop5 = orderTableMapper.selectPayTop5File(user.getUserId());
        for (TopProductVo topProductVo : payTop5) {
            if (topProductVo.getType().equals(1)){
                log.info("查询文集信息");
                AnthologyTable anthologyTable1 = anthologyTableMapper.selectByPrimaryKey(topProductVo.getProductId());
                topProductVo.setProductName(anthologyTable1.getAnthologyName());
            }
        }
        map.put("payTop5",payTop5);

        log.info("查询访客榜前5的商品");
        List<TopProductVo> viewProductVos = orderTableMapper.selectFangKeTop5File(user.getUserId());
        map.put("viewTop5",viewProductVos);

        return map;
    }
}
